package com.simple.common.utils;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.config.ConfigureBuilder;
import com.deepoove.poi.data.AttachmentRenderData;
import com.deepoove.poi.data.AttachmentType;
import com.deepoove.poi.data.Attachments;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.deepoove.poi.policy.AttachmentRenderPolicy;

import java.io.File;
import java.util.Map;

public class WordUtil {

    /**
     * 自己输入存储地址,永久存储
     * @param templatePath
     * @param fileName
     * @param storePath
     * @param fillData
     * @param attachMap 附件map key是文本标签的值,value是附件的全路径
     * @param listMap 列表map key是文本标签的值,value是list
     */
    public static String fillWordWithTemplate(String templatePath,String fileName,String storePath, Map<String,Object> fillData,Map<String,String> attachMap,Map<String,Object> listMap){
        XWPFTemplate render = getTemplateReaderWithList(templatePath,fillData,listMap);
        fileName = changeNameToDocx(fileName);
        File newWord = new File(storePath + fileName);
        writeToNewWord(newWord,render);

        render = getTemplateReaderWithAttach(newWord.getAbsolutePath(), fillData, attachMap);
        writeToNewWord(newWord,render);
        return newWord.getAbsolutePath();
    }

    /**
     * 自己输入存储地址,永久存储
     * @param templatePath 模板地址
     * @param fileName 生成文件名
     * @param storePath 生成文件后存储地址
     * @param fillData 需要填充的数据
     * @return 存储地址完整路径
     */
    public static String fillWordWithTemplate(String templatePath,String fileName,String storePath, Map<String,Object> fillData){
        XWPFTemplate render = getTemplateReader(templatePath,fillData);
        fileName = changeNameToDocx(fileName);
        File newWord = new File(storePath + fileName);
        writeToNewWord(newWord,render);
        return newWord.getAbsolutePath();
    }

    public static void writeToNewWord(File newWord, XWPFTemplate render) {
        try {
            if (!newWord.exists()) {
                newWord.createNewFile();
            }
            render.writeToFile(newWord.getAbsolutePath());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String changeNameToDocx(String fileName) {
        if (fileName.contains(".doc")){
            String[] split = fileName.split("\\.");
            fileName = split[0] + ".docx";
        }
        return fileName;
    }

    /**
     *
     * @param templatePath
     * @param fillDate
     * @param attachMap key不能特殊字符开头,不能数字结尾,建议全英文
     * @return
     */
    private static XWPFTemplate getTemplateReaderWithAttach(String templatePath, Map<String, Object> fillDate,Map<String ,String> attachMap){
        File wordTemplate = new File(templatePath);
        ConfigureBuilder builder = Configure.builder();
        AttachmentRenderPolicy attachPolicy = new AttachmentRenderPolicy();
        AttachmentRenderData attach;
        for (String tagName : attachMap.keySet()){
            attach = Attachments.ofLocal(attachMap.get(tagName), AttachmentType.XLSX).create();
            fillDate.put(tagName,attach);
            builder.bind(tagName, attachPolicy);
        }

        return XWPFTemplate.compile(wordTemplate, builder.build()).render(fillDate);
    }

    public static XWPFTemplate getTemplateReaderWithList(String templatePath, Map<String, Object> fillDate,Map<String,Object> listMap){
        File wordTemplate = new File(templatePath);
        ConfigureBuilder builder = Configure.builder();
        LoopRowTableRenderPolicy loopRowTablePolicy = new LoopRowTableRenderPolicy();
        for (String tagName : listMap.keySet()){
            fillDate.put(tagName,listMap.get(tagName));
            builder.bind(tagName,loopRowTablePolicy);
        }

        return XWPFTemplate.compile(wordTemplate, builder.build()).render(fillDate);
    }

    private static XWPFTemplate getTemplateReader(String templatePath, Map<String, Object> fillDate) {
        File wordTemplate = new File(templatePath);
        ConfigureBuilder builder = Configure.builder();
        return XWPFTemplate.compile(wordTemplate, builder.build()).render(fillDate);
    }


}
